/*Guys, this works well if 
 * 
 * 1. The TaskManager class exist 
 * 2. The Task Class exist
 * 3. The DateUtils class exist for automatic date entry
 * 4. The TaskManager has a minimum of
 * * a. An arraylist os Tasks
 * * b. Three methods with signatures...
 * 			void addTask(String description, String typeText, String due)
 * 			void deleteTask(int index)
 * 			void setIsDone(int index, boolean bool)
 * 5. The Task Class has getters and setters for its attributes
 * 
 * */

package taskApplication.ui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

import taskApplication.model.PeriodicTask;
import taskApplication.model.TaskManager;


public class UiTaskManager extends JFrame implements ActionListener{
	/**
	 * 
	 */
	private static final long serialVersionUID = 8817004418325682802L;

	JPanel mainPan;

	//Contains a task manager object
	private TaskManager manager;
	
	JPanel leftPanel;
	JPanel taskDescrip;
	JPanel taskType;
	JPanel taskPeriod;
	JPanel taskDue;
	JPanel taskAdd;
	
	JPanel rightPanel;
	JPanel header;
	
	JLabel taskDescLab;
	JLabel taskTypeLab;
	JLabel taskPeriodLab;
	JLabel taskDueLab;
	JLabel taskAddLab;
	
	JLabel deleteLab;
	JLabel descripLab;
	JLabel doneLab;
	JLabel dueLab;
	
	JTextArea taskText;
	JComboBox typeBox;
	JComboBox periodBox;
	JTextField dateText;
	JButton addButton;
	
	public UiTaskManager(){
		
		this.manager = new TaskManager();
		this.setUp();
		
	}
	
	public UiTaskManager(TaskManager manager){
		
		this.manager = manager;
		this.setUp();
		
	}
	
	public void setUp(){
		setSize(1000,350);
		this.setTitle("The Task Manager");
		
		setLayout(new BorderLayout());
		mainPan = new JPanel(new GridLayout(1,2));
		leftPanel = new JPanel(new GridLayout(8,1));
		leftPanel.setSize(400, 150);
		taskDescrip = new JPanel(new GridLayout(1,2));
		taskType = new JPanel(new GridLayout(1,2));
		taskPeriod = new JPanel(new GridLayout(1,2));
		taskDue = new JPanel(new GridLayout(1,2));
		taskAdd = new JPanel(new GridLayout(1,3));
		
		taskDescLab = new JLabel("Task Description:");
		taskTypeLab = new JLabel("Task Type:");
		taskPeriodLab = new JLabel("Period:");
		taskDueLab = new JLabel("Due Date:");
		taskAddLab = new JLabel("Add Task");
		
		taskText = new JTextArea(5, 100);
		taskText.setLineWrap(true);
		taskText.setWrapStyleWord(true);
		
		JScrollPane scrollTask = new JScrollPane(taskText);
		
		
		
		String[] comboStrings = TaskManager.getAllTaskTypes();
		typeBox = new JComboBox(comboStrings);
		typeBox.setSelectedIndex(0);
		String[] periods = PeriodicTask.getAllPeriod();
		periodBox = new JComboBox(periods);
		periodBox.setSelectedIndex(0);
		periodBox.setEnabled(false);
		
		dateText = new JTextField(DateUtils.now("MM/dd/yy"));
		addButton = new JButton(taskAddLab.getText());
		
		leftPanel.setBorder(BorderFactory.createTitledBorder("Add Task"));
		
		taskDescrip.add(taskDescLab);
		taskDescrip.add(scrollTask);
		taskType.add(taskTypeLab);
		taskType.add(typeBox);
		taskPeriod.add(taskPeriodLab);
		taskPeriod.add(periodBox);
		taskDue.add(taskDueLab);
		taskDue.add(dateText);
		taskAdd.add(new JLabel());
		taskAdd.add(new JLabel());
		taskAdd.add(addButton);
		
		leftPanel.add(taskDescrip);
		leftPanel.add(taskType);
		leftPanel.add(taskPeriod);
		leftPanel.add(taskDue);
		leftPanel.add(taskAdd);
		
		mainPan.add(leftPanel);
		
		add(mainPan, BorderLayout.CENTER);
		add(new JLabel(" "), BorderLayout.NORTH);
		add(new JLabel(" "), BorderLayout.SOUTH);
		add(new JLabel(" "), BorderLayout.WEST);
		add(new JLabel(" "), BorderLayout.EAST);
		
		rightPanel = new JPanel(new GridLayout(15,1));
		rightPanel.setBorder(BorderFactory.createTitledBorder("Task(s)"));
		
		deleteLab = new JLabel("");
		doneLab = new JLabel("Done");
		descripLab = new JLabel("Description");
		dueLab = new JLabel("Due");
		
		header = new JPanel(new GridLayout(1,4));
		header.add(deleteLab);
		header.add(doneLab);
		header.add(descripLab);
		header.add(dueLab);
		
		rightPanel.add(header);
		
		mainPan.add(rightPanel);	
		
		addButton.addActionListener(this);
		
		typeBox.addActionListener(this);
		//pack();
		this.setVisible(true);
	}

	public void actionPerformed(ActionEvent arg0) {
		// TODO Auto-generated method stub
		System.out.println(arg0.getActionCommand());
		if(arg0.getActionCommand().equalsIgnoreCase("comboBoxChanged")){
			if(typeBox.getSelectedIndex() == 1){
				periodBox.setEnabled(true);
				dateText.setEnabled(false);
			}
			else {
				periodBox.setEnabled(false);
				dateText.setEnabled(true);
			}
			
		}
		
		if(arg0.getActionCommand().contains("dl_index")){
			int index = Integer.parseInt(arg0.getActionCommand().split("dl_index")[1]);
			this.deleteTask(index);
		}
		
		if(arg0.getActionCommand().contains("ch_index")){
			int index = Integer.parseInt(arg0.getActionCommand().split("ch_index")[1]);
			this.setIsDone(index, true);
		}
		
		if(arg0.getActionCommand().equalsIgnoreCase("Add Task")){
			this.addTask();
			taskText.setText("");
		}
	}

	 
	public void addTask(){
		String description = taskText.getText();
		String typeText = (String) typeBox.getSelectedItem();
		String due = dateText.isEnabled() ? dateText.getText() :  (String) periodBox.getSelectedItem() ;
		
		System.out.println("Addidng task to Task Manager: \n" + description + "\n" + typeText + "\n" + due);
		manager.addTask(description, typeText, due);
		this.update();
	}
	
	public void deleteTask(int index){
		System.out.println("Deleting task from Task Manager");
		manager.deleteTask(index);
		this.update();
	}
	
	public void setIsDone(int index, boolean bool) {
		System.out.println("Setting a task to be done in the Task Mamager");
		manager.setIsDone(index, bool);
		this.update();
	}
	
	public void update(){
		rightPanel.removeAll();
		rightPanel.add(header);
		for (int i=0; i<manager.getTasks().size(); i++){
			JPanel panel = new JPanel(new GridLayout(1,4));
			
			JButton delete = new JButton("Delete");
			delete.setBackground(Color.GRAY);
			delete.setActionCommand("dl_index" + i);
			delete.addActionListener(this);
			
			JCheckBox check;
			if (manager.getTasks().get(i).isDone())
				check = new JCheckBox("", true);
			else 
				check = new JCheckBox();
			check.setActionCommand("ch_index" + i);
			check.addActionListener(this);
			
			JLabel desc = new JLabel(manager.getTasks().get(i).getDescription());
			
			JLabel due = new JLabel(manager.getTasks().get(i).getDue());
			
			panel.add(delete);
			panel.add(check);
			panel.add(desc);
			panel.add(due);
			int ct = 0;
			System.out.println(ct++);
			rightPanel.add(panel);
		}
		this.setVisible(false);
		this.setVisible(true);
	}
}
